$(function () {


    // 初始化搜索表单
    function initSearchForm() {

    }

    var _dataTable;
    // 加载数据
    function loadTableData() {
        _dataTable =
            $('#dataTable').DataTable({
                serverSide: true,       // 开启服务端模式
                lengthChange: false,    // 是否允许用户改变表格每页显示的记录数
                searching: false,       // 是否允许开启本地搜索
                paging: true,           // 是否允许表格分页
                processing: true,       // 是否显示正在处理的状态
                ordering: false,         // 是否允许DataTables排序
                // stripeClasses: ["odd", "even"],  // 为奇偶行加上样式，兼容不支持CSS伪类的场合
                autoWidth: false,  //禁用自动调整列宽
                order: [],  //取消默认排序查询,否则复选框一列会出现小箭头
                orderMulti: true,  //启用多列排序
                pagingType: "simple_numbers",  //分页样式：simple,simple_numbers,full,full_numbers
                ajax: function (data, callback, settings) {
                    var pageNumber = data['start'] / data['length'] + 1;
                    var page = {
                        pageNumber: pageNumber,
                        pageSize: data['length']
                    };
                    var parameterMap = {};
                    var formDataItems = $('form.js-form').serializeArray();
                    $.map(formDataItems, function (item) {
                        parameterMap[item['name']] = item['value'];
                    });
                    var data1 = {
                        "page.pageNumber": pageNumber,
                        "page.pageSize": data['length'],
                        "key1": 1,
                        "key2": [1,2,3]
                    };
                    console.info(data1);

                    // 请求参数
                    $.ajax({
                        url: G_CONTEXT_PATH + '/admin/user/page',
                        cache: false,
                        data: data1,
                        dataType: 'json',
                        success: function (result) {
                            result['draw'] = data['draw'];
                            callback(result);
                        },
                        error: function (xhr, status, err) {
                        }
                    });
                },
                columns: [
                    {data: 'id'},
                    {data: 'username'},
                    {data: 'password'},
                    {data: null}
                ],
                columnDefs: [{
                    targets: -1,
                    render: function (data, type, row) {
                        return $('<div></div>')
                            .append('<button class="js-table-operation .js-table-operation-view btn btn-primary">查看</button>')
                            .append('<button class="js-table-operation .js-table-operation-update btn btn-primary">修改</button>')
                            .html();
                    }
                }]
            });
        $('#dataTable').on('click', '.js-table-operation', function () {
            var rowData = dataTable.row($(this).closest('tr')).data();
            console.info(rowData);
        });
    }

    $('form .js-form-operation-search').click(function () {
        _dataTable.draw();
    });


    loadTableData();

});
